//
//  HMNewfeatureCollectionViewController.m
//  黑马彩票
//
//  Created by teacher on 15/3/3.
//  Copyright (c) 2015年 itheima. All rights reserved.
//

#import "HMNewfeatureCollectionViewController.h"
#import "HMCollectionViewCell.h"

@interface HMNewfeatureCollectionViewController ()
@property (nonatomic, weak)UIImageView *guide;
@property (nonatomic, weak)UIImageView *largeText;
@property (nonatomic, weak)UIImageView *smallText;
@property (nonatomic, strong)UIPageControl *page;
@end
/*
 CollectionViewController使用步骤:
 1.创建控制器并传递layout
 2.通过layout设置cell大小, 间隙, 滚动方向
 3.实现数据源方法
 4.注册cell
 5.填充数据
 */
@implementation HMNewfeatureCollectionViewController

static NSString *identifier = @"item";

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UINib *nib = [UINib nibWithNibName:@"HMCollectionViewCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:identifier];

    // 1.设置分页
    self.collectionView.pagingEnabled = YES;
    
    // 2.去除滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    // 3.去除回弹效果
    self.collectionView.bounces = NO;
    
    self.page=[[UIPageControl alloc] initWithFrame:CGRectMake((iphoneWidth-200)/2, iphoneHeight-70, 200, 30)];
    [self.view addSubview:self.page];
    self.page.numberOfPages=4;
    self.page.currentPage=0;
    [self.page addTarget:self action:@selector(pageControlClick) forControlEvents:UIControlEventTouchUpInside];
    
//    // 1.添加线条
//    UIImageView *guideLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]];
//    guideLine.x = -20;
//    [self.collectionView addSubview:guideLine];
    
   
}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    CGFloat offset = scrollView.contentOffset.x;
//    // 1.计算当前的页码
//    
//}


// 告诉系统有3个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}
// 告诉系统每个item显示什么
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 注意: 使用该方法必须先注册一个cell, 否则会报错
    // 1.创建cell
    HMCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    // 2.设置数据
    [cell setIndexPath:indexPath count:4];
    
    // 3.返回cell
    
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page=scrollView.contentOffset.x/iphoneWidth;
    if ((scrollView.contentOffset.x-iphoneWidth*page)>0.5*iphoneWidth) {
        _page.currentPage=page+1;
    }else{
        _page.currentPage=page+0;
    }
}
- (void)pageControlClick
{
    [self.collectionView setContentOffset:CGPointMake(iphoneWidth*self.page.currentPage, 0) animated:YES];
}
@end
